using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace _2_anzor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.mPen = new Pen(Color.Magenta, 1.0f);

            int clipRectangleSide = 178;

            this.clippingRectangle = new Rectangle(
                                    this.panel1.Width - clipRectangleSide,
                                    this.panel1.Height - clipRectangleSide,
                                    clipRectangleSide,
                                    clipRectangleSide);

            this.comboBox1.SelectedIndex = 0;

            this.mLines = new List<SmartLine>();

        }

        List<SmartLine> mLines;

        void CreateLines()
        {
            int x = 0;
            int y = 0;

            int w = this.panel1.Width / this.sections;

            SmartLine line;

            for (int i = 0; i < this.sections; i++)
            {
                for (int k = 0; k < this.panel1.Height / 2; k++)
                {
                    line = new SmartLine();
                    line.StartingPoint = new Point(x, y);
                    line.EndingPoint = new Point(x + w, y);

                    line.BoundingBox = new Rectangle(x, y, w, 1);

                    this.mLines.Add(line);
                    y += 2;
                }

                x += w;
                y = 0;
            }

            this.numberOfLines = this.mLines.Count;

            this.label1.Text = this.numberOfLines.ToString() + " have been created";
            this.label1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.panel1.CreateGraphics();

            // check if there is clipping region to use
            if (this.comboBox1.SelectedIndex > 0)
            {
                graphics.SetClip(this.clippingRectangle);
            }

            int w = this.panel1.Width / this.sections;
            this.linesDrawn = 0;
            long totalMiliseconds = 0;

            if (this.comboBox1.SelectedIndex <= 1)
            {
                for (int i = 0; i < this.numberOfTimesToRepeat; i++)
                {
                    totalMiliseconds += DrawLine_Method1(graphics);
                }
            }

            else if (this.comboBox1.SelectedIndex == 2)
            {
                for (int i = 0; i < this.numberOfTimesToRepeat; i++)
                {
                    totalMiliseconds += DrawLine_Method2(graphics);
                }
            }

            this.label2.Text = linesDrawn.ToString() + " DrawLine() commands took avg " + (totalMiliseconds / (double)this.numberOfTimesToRepeat) + "ms to complete";
        }

        private long DrawLine_Method1(Graphics graphics)
        {
            Stopwatch timer = new Stopwatch();
            graphics.Clear(Color.Green);

            // start the timer
            timer.Start();

            foreach (SmartLine line in this.mLines)
            {
                graphics.DrawLine(this.mPen, line.StartingPoint, line.EndingPoint);
                this.linesDrawn++;
            }

            // stop the timer
            timer.Stop();

            return timer.ElapsedMilliseconds;
        }

        private long DrawLine_Method2(Graphics graphics)
        {
            Stopwatch timer = new Stopwatch();
            graphics.Clear(Color.Green);

            // start the timer
            timer.Start();

            // TODO: USE THE BOUNDING BOXES TO FILTER THEM OUT

            foreach (SmartLine line in this.mLines)
            {
                if (graphics.ClipBounds.IntersectsWith(line.BoundingBox))
                {
                    graphics.DrawLine(this.mPen, line.StartingPoint, line.EndingPoint);
                    this.linesDrawn++;
                }
            }

            // stop the timer
            timer.Stop();

            return timer.ElapsedMilliseconds;
        }

        Pen mPen;

        private void Form1_Load(object sender, EventArgs e)
        {
            numberOfLines = this.sections * (this.panel1.Height / 2); 
        }

        int numberOfLines;
        int sections = 80;
        int numberOfTimesToRepeat = 100;
        int linesDrawn;
        
        Rectangle clippingRectangle;

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*switch (this.comboBox1.SelectedIndex)
            {
                case 0:
                    {
                        this.label1.Text = numberOfLines.ToString() + " lines will be drawn *with no* a clipping region";
                        break;
                    }

                case 1:
                    {
                        this.label1.Text = numberOfLines.ToString() + " lines will be drawn *with* a clipping region";
                        break;
                    }

                case 2:
                    {
                        this.label1.Text = numberOfLines.ToString() + " lines will be drawn *with* a clipping region and checks";
                        break;
                    }
            }*/
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.panel1.Invalidate();
            this.label2.Text = "";
        }

        struct SmartLine
        {
            public Rectangle BoundingBox;
            public Point StartingPoint;
            public Point EndingPoint;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.CreateLines();
            this.button1.Enabled = true;
            this.button2.Enabled = true;
            this.button3.Visible = false;
        }
    }
}